package com.guanmi.ethereum.configure;

import com.guanmi.ethereum.service.BloggerCoin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.EthFilter;

import java.math.BigInteger;

/**
 * 服务监听器，继承ApplicationRunner，在spring启动时启动
 *
 * @author liqiang
 */
@Component
public class ServiceRunner implements ApplicationRunner {

    /**
     * 智能合约部署地址
     */
    private String contractAddress = "0x1F999F370c30Ddf49fFd795E5A65c392f1aA85a5";

    @Autowired
    private BloggerCoin bloggerCoin;

    @Override
    public void run(ApplicationArguments var1) {
        EthFilter ethFilter = new EthFilter(DefaultBlockParameter.valueOf(new BigInteger("46")), DefaultBlockParameterName.LATEST, contractAddress);
        bloggerCoin.transferEventFlowable(ethFilter).subscribe(response -> {
            Boolean effective = response.effective;
            if (!effective) {
                return;
            }
            System.out.println(response.log.getBlockNumber());
            System.out.println(response.log.getTransactionHash());
            System.out.println(response.to);
            System.out.println(response.from);
            System.out.println(response.value);
            System.out.println("=============response===========");
        });
    }
}